package com.wshl.aijiuunion.view.widget;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.LogUtils;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.wshl.aijiuunion.NetUrl;
import com.wshl.aijiuunion.R;
import com.wshl.aijiuunion.view.bean.AreaListBean;
import com.wx.wheelview.adapter.ArrayWheelAdapter;
import com.wx.wheelview.widget.WheelView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/11/15.
 */

public class AreaDialog extends Dialog {

    Context mContext;
    @BindView(R.id.wheelview5)
    WheelView wheelview5;
    @BindView(R.id.wheelview6)
    WheelView wheelview6;
    @BindView(R.id.wheelview7)
    WheelView wheelview7;
    @BindView(R.id.wheelview8)
    WheelView wheelview8;
    @BindView(R.id.ll_list)
    LinearLayout llList;
    @BindView(R.id.ll_select)
    LinearLayout llSelect;
    @BindView(R.id.tv_area_cancel)
    TextView tvAreaCancel;
    @BindView(R.id.tv_area_confirm)
    TextView tvAreaConfirm;
    private List<String> options1Items = new ArrayList<>();
    private List<String> options2Items = new ArrayList<>();
    private List<String> options3Items = new ArrayList<>();
    private List<String> options4Items = new ArrayList<>();
    private String selsectItem1, selsectItem2, selsectItem3, selsectItem4;
    private String selsectItem1id, selsectItem2id, selsectItem3id, selsectItem4id;


    private String pid = "0";//第一次请求时传参数0
    private onNoOnclickListener noOnclickListener;
    private onYesOnclickListener yesOnclickListener;

    public AreaDialog(@NonNull Context context) {
        super(context);
        mContext = context;
    }

    public AreaDialog(Context context, int themeResId) {
        super(context, themeResId);
        mContext = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_area);
        ButterKnife.bind(this);
        initView();
        getAreaList(pid, 0);
    }


    //初始化view
    private void initView() {

        for (int i = 0; i < 8; i++) {
            options1Items.add("加载中..");
        }

        wheelview5.setWheelAdapter(new ArrayWheelAdapter(mContext));
        wheelview5.setWheelData(options1Items);
        wheelview5.setSkin(WheelView.Skin.Holo); // common皮肤
        wheelview5.setWheelSize(5);
        //wheelview5.setSelection(4);

        WheelView.WheelViewStyle style = new WheelView.WheelViewStyle();
        style.textColor = Color.parseColor("#333333");
        style.textSize = 14;
        style.selectedTextSize = 14;
        style.holoBorderColor = Color.parseColor("#57BE6A");
        style.backgroundColor = Color.WHITE;
        style.selectedTextColor = Color.parseColor("#57BE6A");
        wheelview5.setStyle(style);

        wheelview6.setWheelAdapter(new ArrayWheelAdapter(mContext));
        wheelview6.setWheelData(options1Items);
        wheelview6.setSkin(WheelView.Skin.Holo); // common皮肤
        wheelview6.setWheelSize(5);
        wheelview6.setStyle(style);

        wheelview7.setWheelAdapter(new ArrayWheelAdapter(mContext));
        wheelview7.setWheelData(options1Items);
        wheelview7.setWheelSize(5);
        wheelview7.setSkin(WheelView.Skin.Holo); // common皮肤
        wheelview7.setStyle(style);

        wheelview8.setWheelAdapter(new ArrayWheelAdapter(mContext));
        wheelview8.setWheelData(options1Items);
        wheelview8.setSkin(WheelView.Skin.Holo); // common皮肤
        wheelview8.setWheelSize(5);
        wheelview8.setStyle(style);
    }

    private void getAreaList(String pid, final int flag) {

        OkGo.<String>post(NetUrl.AREA)
                .params("pid", pid)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        LogUtils.i("获取区域列表:" + response.body());

                        AreaListBean areaListBean = GsonUtils.fromJson(response.body(), AreaListBean.class);
                        final List<AreaListBean.DataBean> data = areaListBean.getData();

                        switch (flag) {
                            case 0:
                                options1Items.clear();
                                for (AreaListBean.DataBean datum : data) {
                                   // if (!datum.getName().contains("钓鱼岛")) {
                                        options1Items.add(datum.getName());
                                  //  }

                                }

                                wheelview5.resetDataFromTop(options1Items);
                                wheelview5.setOnWheelItemSelectedListener(new WheelView.OnWheelItemSelectedListener() {
                                    @Override
                                    public void onItemSelected(int position, Object o) {
//                                        if (data.get(position).getName().contains("钓鱼岛")) {
//
//                                            data.remove(position);
//                                        }
                                        // ToastUtils.showShort("滚动到:" + position);
                                        selsectItem1 = data.get(position).getName();
                                        selsectItem1id = data.get(position).getId() + "";
                                        getAreaList(data.get(position).getId() + "", 1);

                                    }
                                });

                                getAreaList(data.get(0).getId() + "", 1);

                                break;
                            case 1:
                                options2Items.clear();
                                if (data.size() == 0) {
                                    options2Items.add(" ");
                                    selsectItem2 = "";
                                    selsectItem2id = "";

                                    LogUtils.e("selectItem2的值:" + selsectItem2);
                                    wheelview6.resetDataFromTop(options2Items);
                                    // wheelview7.resetDataFromTop(options3Items);

                                    return;
                                }


                                for (AreaListBean.DataBean datum : data) {
                                    options2Items.add(datum.getName());
                                }


                                wheelview6.resetDataFromTop(options2Items);
                                wheelview6.setOnWheelItemSelectedListener(new WheelView.OnWheelItemSelectedListener() {
                                    @Override
                                    public void onItemSelected(int position, Object o) {
                                        selsectItem2 = data.get(position).getName();
                                        selsectItem2id = data.get(position).getId() + "";
                                        getAreaList(data.get(position).getId() + "", 2);
                                    }
                                });
                                getAreaList(data.get(0).getId() + "", 2);
                                break;
                            case 2:
                                options3Items.clear();
                                if (data.size() == 0) {
                                    options3Items.add(" ");
                                    selsectItem3 = "";
                                    selsectItem3id = "";
                                    LogUtils.e("selectItem3的值:" + selsectItem3);
                                    wheelview7.resetDataFromTop(options3Items);
                                    return;
                                }


                                for (AreaListBean.DataBean datum : data) {
                                    options3Items.add(datum.getName());
                                }
                                LogUtils.i("optionsitems3:" + options3Items.size());

                                wheelview7.resetDataFromTop(options3Items);
                                wheelview7.setOnWheelItemSelectedListener(new WheelView.OnWheelItemSelectedListener() {
                                    @Override
                                    public void onItemSelected(int position, Object o) {

                                        selsectItem3 = data.get(position).getName();
                                        selsectItem3id = data.get(position).getId() + "";

                                        getAreaList(data.get(position).getId() + "", 3);
                                    }
                                });
                                getAreaList(data.get(0).getId() + "", 3);
                                break;
                            case 3:
                              /*  options4Items.clear();

                                for (AreaListBean.DataBean datum : data) {
                                    options4Items.add(datum.getName());
                                }
                                LogUtils.i("设置县");
                                wheelview8.resetDataFromTop(options4Items);
                                wheelview8.setOnWheelItemSelectedListener(new WheelView.OnWheelItemSelectedListener() {
                                    @Override
                                    public void onItemSelected(int position, Object o) {
                                        selsectItem4 = data.get(position).getName();
                                        selsectItem4id = data.get(position).getCode() + "";
                                    }
                                });*/
                                break;
                        }

                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        LogUtils.e("获取区域列表:" + response.code());
                    }
                });
    }

    @OnClick({R.id.tv_area_cancel, R.id.tv_area_confirm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_area_cancel://取消按钮 点击事件
                if (noOnclickListener != null) {
                    noOnclickListener.onNoClick();
                }
                break;
            case R.id.tv_area_confirm:// 确定按钮 点击事件
                if (yesOnclickListener != null) {
                    yesOnclickListener.onYesClick();
                    yesOnclickListener.setProvinceid(selsectItem2id);
                    yesOnclickListener.setCityid(selsectItem3id);
                    yesOnclickListener.setCountryid(selsectItem4id);
                    yesOnclickListener.setAreaid(selsectItem1id);
                    LogUtils.i(selsectItem1 + "," + selsectItem2 + "," + selsectItem3 + "," + selsectItem4);

                    if (TextUtils.isEmpty(selsectItem4)) {//有县/区
                        yesOnclickListener.getItemContent(selsectItem1, selsectItem2, selsectItem3, "");
                        yesOnclickListener.getcontent(selsectItem1 + selsectItem2 + selsectItem3);
                    }
                    if (!TextUtils.isEmpty(selsectItem4)) {//没有县/区
                        yesOnclickListener.getcontent(selsectItem1 + selsectItem2 + selsectItem3 + selsectItem4);
                        yesOnclickListener.getItemContent(selsectItem1, selsectItem2, selsectItem3, selsectItem4);
                    }

                }
                break;
        }
    }


    /**
     * 设置确定按钮和取消被点击的接口
     */
    public interface onYesOnclickListener {
        void onYesClick();

        void getItemContent(String province, String cityname, String countryname, String area);

        void getcontent(String address);

        void setProvinceid(String selsectItem2id);

        void setCityid(String selsectItem3id);

        void setCountryid(String selsectItem4id);

        void setAreaid(String selsectItem1id);
    }

    public interface onNoOnclickListener {
        void onNoClick();

    }

    /**
     * 设置取消按钮的显示内容和监听
     *
     * @param onNoOnclickListener
     */
    public void setNoOnclickListener(onNoOnclickListener onNoOnclickListener) {

        this.noOnclickListener = onNoOnclickListener;
    }


    /**
     * 设置确定按钮的显示内容和监听
     *
     * @param onYesOnclickListener
     */
    public void setYesOnclickListener(onYesOnclickListener onYesOnclickListener) {
        this.yesOnclickListener = onYesOnclickListener;
    }
}
